/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var largestValues = function(root) {
    let res = [];
    let queue = [root];
    while(root!=null&&queue.length){
        let len = queue.length;
        let max = -Infinity
        while(len--){
            let node = queue.pop();
            max =node.val>max?node.val:max
             node.left&&queue.unshift(node.left)
            node.right&&queue.unshift(node.right)
        }
        res.push(max)
    }
    return res
};